package zyy.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import zyy.model.BeanAddress;
import zyy.util.BaseException;
import zyy.util.BusinessException;
import zyy.util.Bus;

public class FrmNewMerchant extends JDialog implements ActionListener {
  private static final long serialVersionUID = 6587416590295403864L;

  private BeanAddress selectAddress = null;
  private Object tblAddressTitles[] = BeanAddress.merchantAddrTitle;
  private Object tblAddressData[][];
  DefaultTableModel tabAddressModel = new DefaultTableModel();
  private JTable dataTableAddress = new JTable(tabAddressModel);
  List<BeanAddress> allAddress = null;

  private JLabel mName = new JLabel("新建商家名");
  private JTextField mNameEdt = new JTextField(20);
  private JButton commit = new JButton("提交");
  private JButton exit = new JButton("退出");
  private JButton newAddr = new JButton("新的地址");
  private JPanel text = new JPanel();
  private JPanel list = new JPanel();
  private JPanel btns = new JPanel();

  public void reload() {
    try {
      allAddress = Bus.addrManager.loadAll();
    } catch (BaseException e) {
      JOptionPane.showMessageDialog(null, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
      return;
    }
    tblAddressData = new Object[allAddress.size()][BeanAddress.userAddrTitle.length];
    for (int i = 0; i < allAddress.size(); i++) {
      for (int j = 0; j < BeanAddress.userAddrTitle.length; j++)
        tblAddressData[i][j] = allAddress.get(i).getCell(j);
    }
    tabAddressModel.setDataVector(tblAddressData, tblAddressTitles);
    this.dataTableAddress.validate();
    this.dataTableAddress.repaint();
  }

  public FrmNewMerchant() {
    // 基础 JDialog 设置
    {
      this.setTitle("外卖助手 管理员 添加新商家");
      this.setSize(895, 500);
      this.setLocationRelativeTo(null);
      this.setVisible(true);
    }
    // this.getContentPane() 一级布局
    {
      this.getContentPane().setLayout(new BorderLayout());
      this.getContentPane().add(this.text, BorderLayout.NORTH);
      this.getContentPane().add(this.list, BorderLayout.CENTER);
      this.getContentPane().add(this.btns, BorderLayout.SOUTH);
    }
    // text 相关配置
    {
      this.text.setLayout(new FlowLayout(FlowLayout.LEFT));
      this.text.add(this.mName);
      this.text.add(this.mNameEdt);
    }
    // list 相关配置
    {
      this.list.setLayout(new BorderLayout());
      this.list.add(new JScrollPane(this.dataTableAddress), BorderLayout.CENTER);
      this.dataTableAddress.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
          int i = FrmNewMerchant.this.dataTableAddress.getSelectedRow();
          if (i < 0) {
            return;
          }
          {
            // 同步数据更新和UI更新
            FrmNewMerchant.this.selectAddress = FrmNewMerchant.this.allAddress.get(i);
            System.out.println(FrmNewMerchant.this.selectAddress);
            FrmNewMerchant.this.reload();
            FrmNewMerchant.this.dataTableAddress.setRowSelectionInterval(i, i);
          }
        }
      });
      this.reload();
    }
    // btns 相关配置
    {
      this.btns.setLayout(new FlowLayout(FlowLayout.RIGHT));
      this.btns.add(this.newAddr);
      this.btns.add(this.commit);
      this.btns.add(this.exit);
      this.newAddr.addActionListener(this);
      this.commit.addActionListener(this);
      this.exit.addActionListener(this);
      Bus.jButtonZYYStyle(this.newAddr);
      Bus.jButtonZYYStyle(this.commit);
      Bus.jButtonZYYStyle(this.exit);
    }
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.commit) {
      String addMerchentName = this.mNameEdt.getText();
      if (this.selectAddress == null) {
        JOptionPane.showMessageDialog(null, "请选择一个地址", "错误", JOptionPane.ERROR_MESSAGE);
      }
      try {
        int selAddrID = this.selectAddress.getAddressID();
        Bus.merchantManager.add(addMerchentName, selAddrID);
        Bus.fAdminMerchant.reloadMerchantTable();
      } catch (BusinessException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
      }
      this.setVisible(false);
    } else if (e.getSource() == this.newAddr) {
      // FrmAddrReg fAddrReg = new FrmAddrReg("商家");
      new FrmAddrReg("商家");
      // fAddrReg.setVisible(true);
      // this.reload();
    } else if (e.getSource() == this.exit) {
      this.setVisible(false);
    }
  }
}